/**
*****************************************************************************
*  Copyright (C), 2021-2025, CaryXiong
*
*  @file    wuziqi_ver.alpha1.2.0.cpp
*  @brief   A program about Gobang writen by C++.
*
*  @author  CaryXiong
*  @date    2021.12.9
*  @version Ver.Alpha 1.2.0
*****************************************************************************
*/
————————————————

                            版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。
                        
原文链接：https://blog.csdn.net/winafa/article/details/115693392





#include <stdio.h>
#include <conio.h>
#include <windows.h>

int start_boot() {
	char a;
	int m_one = 0;

	printf("->开始游戏\n");
	printf("  退出游戏\n");

	while (true) {
		a = getch();
		system("cls");
		if (a == ' ') {
			if (m_one == 0) {
				return 1;
			}
			if (m_one == 1) {
				printf("已退出游戏，按任意键关闭窗口");
				return 0;
			}
		}
		if (a == 'w')
			m_one--;
		if (a == 's')
			m_one++;

		if (m_one > 1)
			m_one = 0;
		if (m_one < 0)
			m_one = 1;

		if (m_one == 0) {
			printf("->开始游戏\n");
			printf("  退出游戏\n");
		}
		if (m_one == 1) {
			printf("  开始游戏\n");
			printf("->退出游戏\n");
		}
	}
}

void welcome_boot() {
	printf("制作人：\n");
	printf("        熊志康\n");
	printf("        高一<12>班\n\n\n");
	printf("在此感谢高一<11>班万成羿同学为本人作技术指导！！\n");

	Sleep(3000);
	system("cls");

	printf ("88       88       88   88888888888	 88          88888888888     888888888888    888888888888     8888888888\n");
	printf ("88      8888      88   88                88          88              88        88   88    88    88   88		    \n");
	printf (" 88    88  88    88    88                88          88              88        88   88    88    88   88		    \n");
	printf (" 88    88  88    88    88888888888	 88          88              88        88   88    88    88   88888888888\n");
	printf ("  88  88    88  88     88                88          88              88        88   88    88    88   88		    \n");
	printf ("  88  88    88  88     88                88          88              88        88   88    88    88   88		    \n");
	printf ("   8888      8888      88888888888	 8888888888  88888888888     888888888888   88    88    88    8888888888\n");

	Sleep(3000);
	system("cls");
}

int system_boot() {
	char a;
	char g[20][20];
	char t[20][20];
	int i, j, m, n, o, p;
	int times = 0;
	i = j = m = n = o = p = 0;

	for (i = 0; i <= 19; i++) {
		for (j = 0; j <= 19; j++) {
			g[i][j] = ' ';
			t[i][j] = ' ';
		}
	}

	i = j = 0;

	g[i][j] = '+';

	printf("    ");
	for (int k = 0; k <= 19; k++) {
		printf("%2d|", k+1);
	}
	printf("\n");
	for (m = 0; m <= 19; m++) {

		printf("[%2d]", m+1);
		for (n = 0; n <= 19; n++) {
			printf(" %-1c|", g[m][n]);
		}
		printf("\n");
	}
//输出原始棋盘
	i = j = 0;

	while (true) {
		a = getch();
		system("cls");
		if (a == 'w')
			i--;
		if (a == 's')
			i++;
		if (a == 'a')
			j--;
		if (a == 'd')
			j++;
//WASD控制

		if (i > 19)
			i = 0;
		if (i < 0)
			i = 19;
		if (j > 19)
			j = 0;
		if (j < 0)
			j = 19;
//判断越界

		for (m = 0; m <= 19; m++) {
			for (n = 0; n <= 19; n++) {
				g[m][n] = ' ';
			}
		}

		for (o = 0; o <= 19; o++) {
			for (p = 0; p <= 19; p++) {
				g[o][p] = t[o][p];
			}
		}
//清空棋盘

		g[i][j] = '+';

		if (a == 'j') {

			if (times > 1)
				times = 0;
			if (times < 0)
				times = 1;

			if (t[i][j] != 'X' && t[i][j] != '#') {
				times++;
				if (times == 1)
					g[i][j] = 'X';
				if (times == 2)
					g[i][j] = '#';
				t[i][j] = g[i][j];
			}
		}
//输入待选位置
		printf("    ");
		for (int k = 0; k <= 19; k++) {
			printf("%2d|", k+1);
		}
		printf("\n");
		for (m = 0; m <= 19; m++) {
			printf("[%2d]", m+1);
			for (n = 0; n <= 19; n++) {
				if (g[m][n] == '+') {
					printf("[%-1c]", t[m][n]);
				} else {
					printf(" %-1c|", g[m][n]);
				}

			}
			printf("\n");
		}
//输出最终棋盘

//开始判断五子连珠
		int x, y;
		int sum_x, sum_g;
		for (x = 0; x <= 19; x++) {
			for (y = 0; y <= 19; y++) {
				if (t[x][y] == 'X')
					sum_x++;
				if (t[x][y] == '#')
					sum_g++;
				if (t[x][y] != 'X')
					sum_x = 0;
				if (t[x][y] != '#')
					sum_g = 0;

				if (sum_x == 5) {
					printf("在坐标：%d,%d处", y+1, x+1) ;
					return 8;
				}

				if (sum_g == 5) {
					printf("在坐标：%d,%d处", y+1, x+1) ;
					return 9;
				}

			}
			sum_x = sum_g = 0;
		}
//横轴五子连珠

		for (y = 0; y <= 19; y++) {
			for (x = 0; x <= 19; x++) {
				if (t[x][y] == 'X')
					sum_x++;
				if (t[x][y] == '#')
					sum_g++;
				if (t[x][y] != 'X')
					sum_x = 0;
				if (t[x][y] != '#')
					sum_g = 0;

				if (sum_x == 5) {
					printf("在坐标：%d,%d处", y+1, x+1) ;
					return 8;
				}

				if (sum_g == 5) {
					printf("在坐标：%d,%d处", y+1, x+1) ;
					return 9;
				}

			}
			sum_x = sum_g = 0;
		}
//纵轴五子连珠

		int n = 0;
		for (x = 0; x <= 15; x++) {
			for (y = 5; y <= 19; y++) {
				if (t[x][y] == 'X') {
					sum_x++;
					for (n = 1; n <= 5; n++) {
						if (t[x + n][y - n] == 'X')
							sum_x++;
					}
				}

				if (t[x][y] == '#') {
					sum_g++;
					for (n = 1; n <= 5; n++) {
						if (t[x + n][y - n] == '#')
							sum_g++;
					}
				}

				if (sum_x == 5) {
					printf("在坐标：%d,%d处", y+1, x+1) ;
					return 8;
				}

				if (sum_g == 5) {
					printf("在坐标：%d,%d处", y+1, x+1) ;
					return 9;
				}

				sum_x = sum_g = 0;
			}

			n = 0;
		}
//左斜五子连珠

		n = 0;
		for (x = 19; x >= 0; x--) {
			for (y = 19; y >= 5; y--) {
				if (t[x][y] == 'X') {
					sum_x++;
					for (n = 1; n <= 5; n++) {
						if (t[x - n][y - n] == 'X')
							sum_x++;
					}
				}

				if (t[x][y] == '#') {
					sum_g++;
					for (n = 1; n <= 5; n++) {
						if (t[x - n][y - n] == '#')
							sum_g++;
					}
				}

				if (sum_x == 5) {
					printf("在坐标：%d,%d处", y+1, x+1) ;
					return 8;
				}

				if (sum_g == 5) {
					printf("在坐标：%d,%d处", y+1, x+1) ;
					return 9;
				}

				sum_x = sum_g = 0;
			}

			n = 0;
		}
//右斜五子连珠	
	}
}

int main() {
	int rt;
	int result;
	int a_exit;

	welcome_boot();

	rt = start_boot();
	if (rt == 0)
		return 0;
//退出程序

	result = system_boot();

	if (result == 8) {
		printf("\n");
		printf("X赢了！！！\n");
	}
	if (result == 9) {
		printf("\n");
		printf("#赢了！！！\n");
	}

	while (true) {
		printf ("按下空格以退出\n");
		a_exit = getch();
		if (a_exit == ' ')break;
	}
}
